﻿#include "subcomponent.h"

#include <QDebug>

SubComponent::SubComponent(QString name) : AbstractComponent(name)
{

}

void SubComponent::operation(int level)
{
    QString space;
    for (int i = 0; i < level; i++) {
        space.append("  ");
    }
    qDebug() << qPrintable(space) << level << ": " << getName() << " operating:";
    int l = level + 1;
    for (int i = 0; i < m_componentVector.size(); i++) {
        m_componentVector[i]->operation(l);
    }
}

void SubComponent::add(AbstractComponent *component)
{
    m_componentVector.push_back(component);
}

void SubComponent::remove(AbstractComponent *component)
{
    for (int i = 0; i < m_componentVector.size(); i++) {
        if (m_componentVector[i]->getName() == component->getName()) {
            m_componentVector.remove(i);
            break;
        }
    }
}

AbstractComponent *SubComponent::getComponent(int i)
{
    AbstractComponent* component = Q_NULLPTR;
    if ((i > 0) && (i < m_componentVector.size())) {
        component = m_componentVector.at(i);
    }
    return component;
}
